var open;
var isClickDelete = "0";
$(function () {
    initTable();
    $("#khrydiv").css("display", "block");
    initTable1(-1);
    $.fn.zTree.init($("#treeDemo"), setting);
    $(".pull-right.pagination-detail").css("display", "none");
    $("#treeDemo").css("height", getHeight());
})
var setting = {
    async: {
        enable: true,
        url: "/jxkh/dept/dragtree.json",
        dataType: "json",
        type: 'get',
        autoParam: ["deptId"],
        dataFilter: function (treeId, parentNode, childNodes) {
            $.each(childNodes, function (i, node) {
                node.drag = true;
            });
            return childNodes.data
        }
    },
    callback: {
        onClick: zTreeOnClick
    },
    data: {
        key: {
            name: "alias"
        },
        simpleData: {
            enable: true,
            idKey: "deptId",
            pIdKey: "parentId",
            rootPId: 0
        }
    },
    edit: {
        enable: true,
        showRemoveBtn: false,
        showRenameBtn: false
    }
};
function zTreeOnClick(event, treeId, treeNode) {
    $("#deptid").val(treeNode.deptId);
    var opt = {
        url: "/jxkh/task/relation/checkList.json",//考官列表
        silent: true,
        query: {
            deptid: treeNode.deptId
        }
    };
    $("#boot_table").bootstrapTable('refresh', opt);
}


var $table = $('#boot_table'),
    $remove = $('#remove'),
    selections = [];

function initTable() {
    $('#boot_table').bootstrapTable({
        idField: "dbid",
        sidePagination: 'server',
        url: '/jxkh/task/relation/checkList.json',//考官列表
        queryParams: {
            taskId: $("#taskId").val(),
            deptid: $("#deptid").val()
        },
        height: getHeight(),
        columns: [
            {
                title: '序号',
                field: 'Number',
                align: 'center',
                formatter: function (value, row, index) {
                    var page = $('#boot_table').bootstrapTable("getOptions");
                    return ((page.pageNumber - 1) * page.pageSize) + index + 1;
                }
            }, {
                title: '考核人',
                field: 'checkername',
                align: 'center',
                valign: 'middle'
            }, {
                field: 'dutyname',
                title: '职务',
                align: 'center'
            }, {
                field: 'dutylevel',
                title: '职务层次',
                align: 'center'
            }, {
                field: 'operate',
                title: '操作',
                align: 'center',
                formatter: function (value, row, index) {
                    return '<a class="remove" href="javascript:deleteCheck(' + row.dbid + ')" title="移除">移除</a>';
                }
            }
        ],
        onClickRow: function (row, td, field, value) {
            if (field == "operate") {
            } else {
                $('.success').removeClass('success');
                $(td).addClass("success");
                viewRelation(row.dbid, row.checkername);
            }
        }
    });
}

function initTable1(checkerdbid) {
    $('#boot_table1').bootstrapTable({
        idField: "dbid",
        sidePagination: 'server',
        url: '/jxkh/task/relation/relationList.json',//考官列表
        queryParams: {
            checkerdbid: checkerdbid
        },
        height: getHeight(),
        columns: [
            {
                title: '序号',
                field: 'Number',
                align: 'center',
                formatter: function (value, row, index) {
                    var page = $('#boot_table').bootstrapTable("getOptions");
                    return ((page.pageNumber - 1) * page.pageSize) + index + 1;
                }
            }, {
                title: '姓名',
                field: 'becheckername',
                align: 'center',
                valign: 'middle'
            }, {
                field: 'dutyname',
                title: '职务',
                align: 'center'
            }, {
                field: 'dutylevel',
                title: '职务层次',
                align: 'center'
            }, {
                field: 'operate',
                title: '操作',
                align: 'center',
                formatter: function (value, row, index) {
                    return '<a class="remove" href="javascript:deleteBeCheck(' + row.dbid + ')" title="移除">移除</a>';
                }
            }
        ]
    });
}
function getHeight() {
    return $(window).height() - $('h1').outerHeight(true) - 125;
}
function deleteCheck(dbid) {
    isClickDelete = "1";
    layer.confirm('确认是否移除当前考核人及考核关系？', {icon: 3, title: '提示'}, function (index) {
        layer.close(index);
        $.ajax({
            type: "post",
            dataType: "json",
            url: "/jxkh/task/relation/deleteCheck/@.json".replace("@", dbid),
            success: function (data) {
                showMessage(data.message.body);
                var opt = {
                    url: "/jxkh/task/relation/checkList.json",//考官列表
                    silent: true,
                    query: {
                        deptid: $("#deptid").val()
                    }
                };
                $("#boot_table").bootstrapTable('refresh', opt);
            }
        })
    });
    isClickDelete = "0";
}
function viewRelation(dbid, name) {
    $("#checkdbid").val(dbid);
    // $("#khry").html("被"+name+"考核的人员列表");
    var opt = {
        url: "/jxkh/task/relation/relationList.json",//考官列表
        silent: true,
        query: {
            checkerdbid: dbid,
            deptid: $("#deptid").val()
        }
    };
    $("#boot_table1").bootstrapTable('refresh', opt);
    $("#bkhradd").on("click", function () {
        var url = '/jxkh/task/relation/addBeChecker.html?checkerdbid=' + dbid + '&deptid=' + $("#deptid").val();
        layer.open({
            type: 2,//0（信息框，默认）1（页面层）2（iframe层）3（加载层）4（tips层）。
            shift: 1,//动画    0-6     css3动画
            closeBtn: 1,       //关闭按钮  0 不显示  style  1和2  标题为 false 2
            btn: ['保存', '取消'],
            shade: [0.8, '#393D49'],
            btn1: function (index, layero) {
                var newpsw = $(layero).find("iframe")[0].contentWindow;
                newpsw.submitForm();

            },
            title: ['新增被考核人', 'background:#5f9ad1;color:#fff;text-align: left;font-size: 18px;'],
            area: ['900px', '550px'], //宽高
            content: url //iframe层-禁滚动条  $("#photosDemo").html()   'http://www.baidu.com/'
        });
    });
}

function deleteBeCheck(dbid) {
    layer.confirm('确认移除?', {icon: 3, title: '提示'}, function (index) {
        layer.close(index);
        $.ajax({
            type: "post",
            dataType: "json",
            url: "/jxkh/task/relation/deleteBeCheck/@.json".replace("@", dbid),
            success: function (data) {

                showMessage(data.message.body);
                var opt = {
                    url: "/jxkh/task/relation/relationList.json",//考官列表
                    silent: true,
                    query: {
                        checkerdbid: $("#checkerdbid").val()
                    }
                };
                $("#boot_table1").bootstrapTable('refresh', opt);
            }
        });
    });


}